package com.itheima.user.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.itheima.user.mapper.UserMapper;
import com.leon.api.pojos.User;
import com.leon.api.service.TokenService;
import com.leon.api.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PolicyHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @program: heima-wallet
 * @author: Leon
 * @date: 2021-03-08 20:01
 **/
@Slf4j
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Override
    public String getCode(String phone) {
        if (StringUtils.isEmpty(phone)){
            return null;
        }
        String s = redisTemplate.opsForValue().get("PHNOE_" + phone);
        if (s!=null){
            return null;
        }
//        生成验证码
        Random random = new Random();
        String code = ""+(random.nextInt(899999)+100000);
        long timeout = 60L;
        redisTemplate.opsForValue().set("PHONE_"+phone,code,timeout, TimeUnit.SECONDS);
        log.error(code);
        return code;
    }

    @Override
    public String login(String phone, String code) {
//        判断手机号验证码是否有效
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            return null;
        }
        String ptoken = "PHONE_" + phone;
        String s = redisTemplate.opsForValue().get(ptoken);
//        判断验证码是否未失效
        if (StringUtils.isEmpty(s)){
            return null;
        }
        if(!StringUtils.equals(s,code)){
            return null;
        }
//        校验通过删除缓存
        redisTemplate.delete(phone);

//        创建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
//        查询手机号是否注册
        User user = this.userMapper.selectOne(queryWrapper);

//        用户不存在
        if (user == null){
            user = new User();
            user.setUsername("为什么不注册就登陆?_"+user.getId());
            user.setPhone(phone);
            user.setPassword("123456");
            // 注册
            userMapper.insert(user);
        }

//        生成token
        String token = tokenService.getToken(user);

        return token;
    }

    @Override
    public Boolean register(String username, String phone, String password, String code) {

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)){
            return false;
        }
//        不想写DTO了谢谢
//        所有参数必须填不准为空，交给前端判断！！！！！！！！！！！！


//        判断手机号是否注册
        //        创建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
//        查询手机号是否注册
        User user = this.userMapper.selectOne(queryWrapper);

        if (user != null){
            return false;
        }

//        判断验证码是否正确
        String ptoken = "PHONE_" + phone;
        String s = redisTemplate.opsForValue().get(ptoken);
        if (!StringUtils.equals(s,code)){
            return false;
        }
        user = new User();
        user.setPhone(phone);
        user.setUsername(username);
        user.setPassword(password);
        user.setAccountId(null);
        user.setCreateTime(new Date());
        userMapper.insert(user);
        return true;
    }
}
